﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Estimate.Common.DataFileValidator
{
    /// <summary>
    /// 降水等值线预报文件合法性检查
    /// </summary>
    public class RainForeValidator : IDataFileValidator
    {
        public ValidateResult ValidateFile(string filepath)
        {
            ValidateResult result = new ValidateResult();
            int lineNumber = 0;
            StreamReader file = new StreamReader(filepath, Encoding.Default);

            string line = file.ReadLine();

            line = file.ReadLine();

            lineNumber = 2;

            List<string> values = new List<string>();
            do
            {
                line = file.ReadLine();
                lineNumber++;

            } while (line.Trim() != "STATION_SITUATION");

            line = file.ReadLine();
            lineNumber++;
            while (line.Substring(0, 14) != "WEATHER_REGION")
            {
                string pattern = @"\ +";
                string[] foreserial = Regex.Split(line, pattern, RegexOptions.IgnoreCase);

                try
                {
                    int r1 = (int)Convert.ToSingle(foreserial[1]);
                }
                catch (FormatException e)
                {
                    string msg = string.Format("第{0}行，第{1}列数据不能转化为float数值类型。", lineNumber, 2);
                    ValidateResultRecord record = new ValidateResultRecord(lineNumber, 2, msg);
                    result.ErrorList.Add(record);
                }
                catch (Exception e)
                {
                    string msg = e.Message;
                    ValidateResultRecord record = new ValidateResultRecord(lineNumber, 2, msg);
                    result.ErrorList.Add(record);
                }

                lineNumber++;
            }
            return result;
        }
    }
}
